#include <iostream>
#include <signal.h>
#include "../comm/httplib.h"
#include "./oj_control.hpp"

using namespace httplib; 
using namespace ns_control;

control* ctl_ptr = nullptr;
void hander(int signo)
{
    ctl_ptr->RecoveryMachine();
}

int main()
{
    signal(SIGQUIT, hander);
    //用户请求的服务路由功能
    Server svr;
    control ctl;
    ctl_ptr = &ctl;
    //获取所有的题目列表
    svr.Get("/all_questions", [&ctl](const Request& rq, Response& resp){
        std::string html;
        ctl.AllQuestions(&html);
        resp.set_content(html, "text/html;charset=utf-8");
        //resp.set_content("这是所有的题目", "text/plain;charset=utf-8");
    });
    //获取用户指定的题目
    svr.Get(R"(/question/(\d+))", [&ctl](const Request& rq, Response& resp){
        std::string Number = rq.matches[1];
        std::string html;
        ctl.Question(Number, &html);
        resp.set_content(html, "text/html;charset=utf-8");
        //resp.set_content("这是用户指定的题目 " + No, "text/plain;charset=utf-8");
    });
    //获取判题功能
    svr.Post(R"(/judge/(\d+))", [&ctl](const Request& rq, Response& resp){
        std::string Number = rq.matches[1];
        std::string out_string;
        ctl.Judge(Number, rq.body, &out_string);
        resp.set_content(out_string, "application/json;charset=utf-8");
        //resp.set_content("指定题目的判题功能 " + Number, "text/plain;charset=utf-8");
    });

    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0", 8084);
    
    return 0;
}
